package org.jxkj.bean;

import org.jxkj.utils.PointFormulaKit;
import org.jxkj.utils.ScriptTool;
import org.jxkj.webapi.WebAPI;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

public class FormaluPoint {
    public Map<Integer, String> formulaPoint(String pointId, String statDate, String endDate, String stat_type, Integer step) throws Exception {
        CmPoint cmPoint = CmPoint.getCmPoint(pointId).get(0);
        System.out.print("cmPoint--->" + cmPoint);
        return getPointValueForFormula(cmPoint.getFormula(), statDate, endDate, stat_type, step);
    }

    public Map<Integer, String> formulaPoint(List<PointData> reset, String pointId, String statDate, String endDate, String stat_type, Integer step) throws Exception {
        CmPoint cmPoint = CmPoint.getCmPoint(pointId).get(0);
        System.out.print("cmPoint--->" + cmPoint);
        return getPointValueForFormula(reset, cmPoint.getFormula(), statDate, endDate, stat_type, step);
    }

    public Map<Integer, String> caclFormula(String formula, String statDate, String endDate, Integer step) throws Exception {
        return getPointValueForFormula(formula, statDate, endDate, "day", step);
    }

    private List<CmPoint> getFormulaPoint(String formula) throws Exception {
        List<String> formulaPointArr = getFormulaPointArr(formula);
        String sql = "point_code in (" + PointFormulaKit.getSqlString(formulaPointArr) + ")";
        System.out.println(sql);
        return CmPoint.queryWhere(sql);
    }

    private List<String> getFormulaPointArr(String formula) {
        String rgex = "data\\[\"(.*?)\"\\]";
        return PointFormulaKit.getSubUtil(formula, rgex);
    }

    private Map<Integer, String> getPointValueForFormula(String formula, String statDate, String endDate, String stat_type, Integer step) throws Exception {
        Map<String, Double> data = new HashMap<>();
        List<CmPoint> refCmPoints = getFormulaPoint(formula);
        refCmPoints.forEach(x -> System.out.println("refCmPoints--" + x));
        Map<String, Object> ctx = new HashMap<>();
        ctx.put("scene", "组合测点计算");
        InputStream is = PointFormulaKit.getStrToStream(formula);
        ScriptTool sc = new ScriptTool(is, ctx);
        List<String> statList = new ArrayList<>();
        refCmPoints.forEach(item -> statList.add(item.getPointCode()));
        List<PointData> list = getWepApiDatal(statList, statDate, endDate);
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Map<Integer, String> timeValueMap = new HashMap<>();
        String start = statDate;
        long starttimestamp = sdfs.parse(statDate).getTime() / 1000L;
        while (start.compareTo(endDate) <= 0) {
            System.out.println("start++" + start);
            if (list.size() > 0) {
                for (PointData pd : list) {
                    if (pd.getTime().equals(start)) {
                        data.put(pd.getName(), Double.valueOf(Double.parseDouble(pd.getValue())));
                    }
                }
            }
            timeValueMap.put(Integer.valueOf((int) starttimestamp), PointFormulaKit.getPointValue(data, sc));
            start = sdfs.format(new Date(starttimestamp * 1000L + (step.intValue() * 1000)));
            starttimestamp += step.intValue();
        }
        System.out.println(timeValueMap);
        return timeValueMap;
    }

    private Map<Integer, String> getPointValueForFormula(List<PointData> reset, String formula, String statDate, String endDate, String stat_type, Integer step) throws Exception {
        List<CmPoint> refCmPoints = getFormulaPoint(formula);
        refCmPoints.forEach(x -> System.out.println("refCmPoints--" + x));
        Map<String, Object> ctx = new HashMap<>();
        ctx.put("scene", "组合测点计算");
        InputStream is = PointFormulaKit.getStrToStream(formula);
        ScriptTool sc = new ScriptTool(is, ctx);
        List<String> statList = new ArrayList<>();
        refCmPoints.forEach(item -> statList.add(item.getPointCode()));

        List<PointData> list = getWepApiDatal(statList, statDate, endDate);

        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Map<Integer, String> timeValueMap = new HashMap<>();
        String start = statDate;
        long starttimestamp = sdfs.parse(statDate).getTime() / 1000L;
        while (start.compareTo(endDate) <= 0) {
            System.out.println("start++" + start);
            Map<String, Double> data = new HashMap<>();
            if (reset.size() > 0) {
                for (PointData rs : reset) {
                    if (rs.getTime().equals(start)) {
                        data.put(rs.getName(), Double.valueOf(Double.parseDouble(rs.getValue())));
                    }
                }
            }
            if (list.size() > 0) {
                for (PointData pd : list) {
                    if (pd.getTime().equals(start)) {
                        data.put(pd.getName(), Double.valueOf(Double.parseDouble(pd.getValue())));
                    }
                }
            }
            System.out.println("data--" + data);
            timeValueMap.put(Integer.valueOf((int) starttimestamp), PointFormulaKit.getPointValue(data, sc));
            start = sdfs.format(new Date(starttimestamp * 1000L + (step.intValue() * 1000)));
            System.out.println("111--" + (int) starttimestamp + "222--" + starttimestamp + "333--" + start);
            starttimestamp += step.intValue();
        }
        return timeValueMap;
    }

    private CmPoint getPointWhereCodeForList(String pointCode, List<CmPoint> list) {
        for (CmPoint cmPoint : list) {
            if (pointCode.equals(cmPoint.getPointCode())) return cmPoint;
        }
        return null;
    }

    private Map<String, Double> getWepApiData(String statList, String statDate) throws Exception {
        WebAPI web = new WebAPI();
        Map<String, Double> data = new HashMap<>();
        List<PointData> list = web.getSnapVals(statList, statDate, statDate, "1");
        for (PointData pd : list) {
            data.put(pd.getName(), Double.valueOf(Double.parseDouble(pd.getValue())));
        }
        return data;
    }

    private Map<String, Double> getWepApiData(List<String> statList, String statDate, String endDate) throws Exception {
        WebAPI web = new WebAPI();
        Map<String, Double> data = new HashMap<>();
        for (String sl : statList) {
            if (!sl.substring(0, 4).equals("HMS_")) {
                List<PointData> list1 = web.getSnapVals(sl, statDate, endDate, "300");
                for (PointData pd : list1) {
                    data.put(pd.getName(), Double.valueOf(Double.parseDouble(pd.getValue())));
                }
                continue;
            }
            List<PointData> list = web.getSnapVals(sl, statDate, endDate, "300");
            for (PointData pd : list) {
                data.put(pd.getName(), Double.valueOf(Double.parseDouble(pd.getValue())));
            }
        }
        System.out.println(data);
        return data;
    }

    private List<PointData> getWepApiDatal(List<String> statList, String statDate, String endDate) throws Exception {
        WebAPI web = new WebAPI();
        List<PointData> list = new ArrayList<>();
        for (String sl : statList) {
            if (!sl.substring(0, 4).equals("HMS_")) {
                list.addAll(web.getSnapVals(sl, statDate, endDate, "300"));
                continue;
            }
            try {
                return list;
            } catch (Exception e) {
                return list;
            } finally {
                Exception exception = null;
            }
        }
        return list;
    }
}
